package com.ruoyi.common.utils.gpsPoint;

import com.ruoyi.common.core.pad.LinePoint;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.util.ArrayList;
import java.util.List;


/**
 * Gps点绘制
 */
public class GpsPoint {

    /**
     * 获取最大半径值
     */
    public double getMaxRadius(List<LinePoint> point) {
        Double maxRadius = 0D;
        //获取第一个点位
        LinePoint pointS = point.get(0);
        for(int i = 1 ; i < point.size(); i++) {
            double radius = getDistanceMeter(pointS, point.get(i));
            maxRadius = Double.max(radius, maxRadius);
        }
        return maxRadius;
    }


    /**
     * 添加单个测点
     *
     * @param point 坐标点集合
     */
    public List<LinePoint> addPointF(List<LinePoint> point) {
        List<LinePoint> linePointList = new ArrayList<>();
        //角度和距离集合
        if(null == point || point.isEmpty()) {
            LinePoint linePoint = addPointF(0D, 0D);
            linePointList.add(linePoint);
        }else {
            //获取第一个点位
            LinePoint pointS = point.get(0);
            LinePoint linePoint = addPointF(0D, 0D);
            linePointList.add(linePoint);
            for(int i = 1 ; i < point.size(); i++) {
                double angle = rotationFunc(pointS, point.get(i));
                double radius = getDistanceMeter(pointS, point.get(i));
                LinePoint linePoints = addPointF(angle, radius);
                linePointList.add(linePoints);
            }
        }
        return linePointList;
    }

    /**
     * 添加单个测点
     *
     * @param angle  角度
     * @param radius 半径
     */
    public LinePoint addPointF(Double angle, Double radius) {
        double radians = Math.toRadians(angle);
        float x = (float) (radius * Math.cos(radians));
        float y = (float) (radius * Math.sin(radians));
        LinePoint point = new LinePoint(x, y);
        return point;
    }


        public double rotationFunc(LinePoint targetPointS, LinePoint targetPointF) {
        // 将经纬度转换为弧度
        double radianLatitude1 = Math.toRadians(targetPointS.x);
        double radianLongitude1 = Math.toRadians(targetPointS.y);
        double radianLatitude2 = Math.toRadians(targetPointF.x);
        double radianLongitude2 = Math.toRadians(targetPointF.y);

        // 计算经纬度差值
        double diffLongitude = radianLongitude2 - radianLongitude1;
        double diffLatitude = radianLatitude2 - radianLatitude1;

        // 计算旋转角度
        double rotationAngle = Math.atan2(diffLatitude, diffLongitude);

        // 将旋转角度转换为度数
        return Math.toDegrees(rotationAngle);
    }

    public double getDistanceMeter(LinePoint targetPointS, LinePoint targetPointF) {
        GlobalCoordinates gpsFrom = new GlobalCoordinates(targetPointS.x, targetPointS.y);
        GlobalCoordinates gpsTo = new GlobalCoordinates(targetPointF.x, targetPointF.y);
        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.WGS84, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }
}
